<?php
require_once("../../../config.inc.php");
$isReloadable = false;
require_once("../../../app/_inc/include.php");
require_once("../_inc/_mbAppDomValue.php");
require_once("domainFunction.php");

$errMsg = "";
$domain = sxREQUEST("domain");
$entity = sxREQUEST("entity");
$attribute = sxREQUEST("attribute");
$description = sxREQUEST("description");
$note = sxREQUEST("note");
$type = sxREQUEST("type");
$length = sxREQUEST("length");
$decimals = sxREQUEST("decimals");
$enumerable = sxREQUEST("enumerable");
$frequent = (sxREQUEST("frequent")=='Y')?'Y':'N';

if(isset($_REQUEST["reqtype"]) && $_REQUEST["reqtype"] == "save"){
	$errMsg .= domainIns();	
}else if(isset($_REQUEST["ent"])){
	$entity = $_REQUEST["ent"];
}
//RS elenco entity
$sSQL = "SELECT * FROM entity ORDER BY entity";
if(!$queryAttr = $objSDAO->query($sSQL,'entList',array("contOnErr"=>true))){
	$errMsg .= $objSDAO->error;
	$errCode .= $objSDAO->errno;
}

if($entity != ""){
	//RS elenco attribute
	$sSQL = "SELECT * FROM attribute WHERE entity='$entity' ORDER BY attribute";
	if(!$queryAttr = $objSDAO->query($sSQL,'attrList',array("contOnErr"=>true))){
		$errMsg .= $objSDAO->error;
		$errCode .= $objSDAO->errno;
	}
}
//output HTML
$aOpz = array("kMenu"=>'dam.dom', "isReloadable"=>$isReloadable);
echo(headerHTML($aOpz));
?>
<script language="JavaScript" type="text/javascript" src="../../_jsc/ajax.js"></script>  
<script language="JavaScript" type="text/javascript">
	function objFrmSave(){
		var errMsg = "";
		var objFrm = document.getElementById("objFrm");
		var objMessArea =  document.getElementById("messArea");
		
		var objDom =  document.getElementById("domain");
		if(objDom.value == ""){
			errMsg += "<?php echo _t("missing domain")?><br>";
			objDom.className = "inpt01";
			objDom.focus();
		}else{
			objDom.className = "inpt01";
		}
		if(errMsg == ""){
			objFrm.submit();
		}else{
			objMessArea.innerHTML = errMsg;
		}		
	}
</script>
  
<?php
echo(bodyHTML($aOpz));
echo("<table width=\"100%\" cellpadding=\"03\" cellspacing=\"0\">\n<tr>\n<td>\n");
//form inserimento dati
echo("<form name=\"objFrm\" id=\"objFrm\" action=\"domainIns.php\" method=\"POST\">");
echo("<input type=\"hidden\" name=\"reqtype\" id=\"reqtype\" value=\"save\">");
echo("<input type=\"hidden\" name=\"xsn\" id=\"xsn\" value=\"".XSN."\">");
//output comandi
$aCmd = array("ins"=>array("pag"=>"javascript:objFrmSave()",
										"title"=>_t("save new domain"),
										"text"=>_t("save"),
										"perm"=>"upddom"),
					"back"=>array("pag"=>$objUser->getHistory(0,false),
										"title"=>_t("cancel operation"),
										"text"=>_t("cancel"))
					);
echo(commandsHTML($aCmd));	
echo("<table class=\"tabl01\" width=\"100%\" cellpadding=\"3\" cellspacing=\"2\">\n");
echo("<tr>" .
			"<td class=\"titl01\" colspan=\"2\">Domain</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">"._t("Domain")." *</td>" .
			"<td class=\"text01\"><input class=\"inpt01\" type=\"text\" name=\"domain\" id=\"domain\" maxlength=\"32\" size=\"32\" value=\"$domain\"></td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">"._t("Description")."</td>" .
			"<td class=\"text01\"><input class=\"inpt01\" type=\"text\" name=\"description\" id=\"description\" maxlength=\"80\" size=\"80\" value=\"".sxHE($description)."\"></td>" .
		"</tr>\n");
echo("<tr>" .
	"<td class=\"labl01\">"._t("Length")." *</td>" .
	"<td class=\"text01\"><input class=\"inpt01\" type=\"text\" name=\"length\" id=\"length\" value=\"$length\"></td>" .
"</tr>\n");
echo("<tr>" .
	"<td class=\"labl01\">"._t("Decimals")."</td>" .
	"<td class=\"text01\"><input class=\"inpt01\" type=\"text\" name=\"decimals\" id=\"decimals\" value=\"$decimals\"></td>" .
"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">"._t("Enumerable")."</td>" .
			"<td class=\"text01\">");
echo("<select name=\"enumerable\" id=\"enumerable\">");
foreach($aEnum as $sEnum=>$sEnumDescr){
	$chk = "";
	if($sEnum == $enumerable)
		$chk = "SELECTED";
	echo("<option value=\"$sEnum\" $chk>".sxHE($sEnumDescr)."</option>");
}
echo("</select>");
echo("</td></tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">"._t("look-up entityp")."</td>" .
			"<td class=\"text01\">");
echo("<select class=\"inpt01\" name=\"lkp_entity\" id=\"lkp_entity\" onChange=\"getChildAttribute('lkp_attribute', 'lkp_entity')\">");
echo("<option value=\"\">  -----  </option>");
while($row = $objSDAO->fetch('entList')){
	$chk = "";
	if($entity == $row["entity"])
		$chk = "SELECTED";
	echo("<option value=\"".$row["entity"]."\" $chk>".$row["entity"]."</option>");
}	
echo("</select>");		
echo("</td></tr>\n");

echo("<tr>" .
			"<td class=\"labl01\">"._t("look-up attribute")."</td>" .
			"<td class=\"text01\">");
echo("<select class=\"inpt01\" name=\"lkp_attribute\" id=\"lkp_attribute\">");
echo("<option value=\"\">  -----  </option>");
if($entity != ""){
	while($row = $objSDAO->fetch('attrList')){
		$chk = "";
		if($attribute == $rowDA["attribute"])
			$chk = "SELECTED";
		echo("<option value=\"".$row["attribute"]."\" $chk>".$row["attribute"]." - ".sxHE($row["description"])."</option>");
	}
}
echo("</select>");
echo("</td></tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">"._t("Type")." *</td>" .
			"<td class=\"text01\">");
echo("<select name=\"type\" id=\"type\" class='inpt01'>");
echo("<option value=\"\">  -----  </option>");
foreach($aDomainType as $sType=>$sTypeDescr){
	$chk = "";
	if($sTypeDescr == $type)
		$chk = "SELECTED";
	echo("<option value=\"$sTypeDescr\" $chk>".sxHE($sTypeDescr)."</option>");
}
echo("</select>");
echo("</td></tr>\n");
echo("<tr>" .
	"<td class=\"labl01\">"._t("Frequent")."</td>" .
	"<td class=\"text01\">" .
	"<input class=\"inpt01\" type=\"radio\" name=\"frequent\" id=\"frequent\" value=\"Y\"".
	(($frequent=='Y')?' CHECKED':'').">"._t('yes')."\n" .
	"<input class=\"inpt01\" type=\"radio\" name=\"frequent\" id=\"frequent\" value=\"N\"".
	(($frequent=='N')?' CHECKED':'').">"._t('no')."\n" .
	"</td>" .
"</tr>\n");
echo("<tr>" .
	"<td class=\"labl01\">"._t("Note")."</td>" .
	"<td class=\"text01\"><textarea class=\"inpt01\" name=\"note\" id=\"note\" cols=\"80\" rows=\"8\">".sxHE($note)."</textarea></td>" .
"</tr>\n");

echo("</table>");

echo(commandsHTML($aCmd));
echo("</form>");
echo("</td>\n</tr>\n</table>\n");
echo(footerHTML());
